import { useMemo } from "react";
import { useTranslation } from "react-i18next";

// 类型定义
interface UseUploadTypeProps {
	accept: string[];
	helpText?: string;
	maxNumber: number;
	maxSize: number;
}

export function useUploadType({ accept, helpText, maxNumber, maxSize }: UseUploadTypeProps) {
	const { t } = useTranslation();

	// 文件类型限制
	const getAccept = useMemo(() => {
		if (accept && accept.length > 0) return accept;

		return [];
	}, [accept]);

	const getStringAccept = useMemo(() => {
		return getAccept
			.map(item => {
				if (item.indexOf("/") > 0 || item.startsWith(".")) return item;
				else return `.${item}`;
			})
			.join(",");
	}, [getAccept]);

	// 支持jpg、jpeg、png格式，不超过2M，最多可选择10张图片。
	const getHelpText = useMemo(() => {
		if (helpText) return helpText;

		const helpTexts: string[] = [];

		if (accept && accept.length > 0) helpTexts.push(t("component.upload.accept", [accept.join(",")]));

		if (maxSize) helpTexts.push(t("component.upload.maxSize", [maxSize]));

		if (maxNumber && maxNumber !== Number.POSITIVE_INFINITY) helpTexts.push(t("component.upload.maxNumber", [maxNumber]));

		return helpTexts.join("，");
	}, [helpText, accept, maxSize, maxNumber, t]);

	return { getAccept, getStringAccept, getHelpText };
}
